package com.youkeyi.ddy.cloud.application.business.crm.order.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.BusinessCustomerOrderFollowCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.dto.BusinessCustomerOrderFollowListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.dto.BusinessCustomerOrderFollowListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.param.BusinessCustomerOrderFollowCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.param.BusinessCustomerOrderFollowDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.param.BusinessCustomerOrderFollowListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.param.BusinessCustomerOrderFollowUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.CrmCustomerOrderFollowUpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.dto.CrmCustomerOrderFollowUpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerOrderFollowUpCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerOrderFollowUpDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerOrderFollowUpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerOrderFollowUpUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.PublicTodoCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoCreateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SystemDateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessCustomerFollowController
 *
 * @Author liuxiawang
 * @Date 2024/3/4 6:04 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessCustomerOrderFollowController implements BusinessCustomerOrderFollowCloudService {

    @Autowired
    private CrmCustomerOrderFollowUpCloudService crmCustomerOrderFollowUpCloudService;
    @Autowired
    private PublicTodoCloudService publicTodoCloudService;

    @Override
    public BusinessCustomerOrderFollowListCloudDto list(BusinessCustomerOrderFollowListCloudParam param) {
        BusinessCustomerOrderFollowListCloudDto dto = new BusinessCustomerOrderFollowListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmCustomerOrderFollowUpQueryCloudParam configQueryCloudParam = new CrmCustomerOrderFollowUpQueryCloudParam();
        configQueryCloudParam.setCustomerFollowUpPrimaryKeys(param.getCustomerOrderFollowUpPrimaryKeys());
        configQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        configQueryCloudParam.setCustomerKeys(param.getCustomerKeys());
        configQueryCloudParam.setOrderKeys(param.getOrderKeys());
        configQueryCloudParam.setStatuses(param.getStatuses());
        configQueryCloudParam.setPageSize(param.getPageSize());
        configQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmCustomerOrderFollowUpListCloudDto configDtos = crmCustomerOrderFollowUpCloudService.query(configQueryCloudParam);
        if (null != configDtos && CollectionUtils.isNotEmpty(configDtos.getRows())) {
            List<BusinessCustomerOrderFollowListItemCloudDto> items = new ArrayList<>();
            configDtos.getRows().forEach(config -> {
                BusinessCustomerOrderFollowListItemCloudDto item = new BusinessCustomerOrderFollowListItemCloudDto();
                item.setCustomerFollowUpPrimaryKey(config.getCustomerFollowUpPrimaryKey());
                item.setCorpKey(config.getCorpKey());
                item.setCustomerKey(config.getCustomerKey());
                item.setCustomerName(config.getCustomerName());
                item.setOrderKey(config.getOrderKey());
                item.setFollowUp(config.getFollowUp());
                item.setFollowUpDate(config.getFollowUpDate());
                item.setNextFollowUpRemindDate(config.getNextFollowUpRemindDate());
                item.setRemindStart(config.getRemindStart());
                item.setStatus(config.getStatus());
                item.setIsDeleted(config.getIsDeleted());
                item.setIsDeletedName(config.getIsDeletedName());
                item.setRemark(config.getRemark());
                item.setServerDate(config.getServerDate());
                item.setAssistingPersonnelKey(config.getAssistingPersonnelKey());
                item.setAssistingPersonnelName(config.getAssistingPersonnelName());
                item.setCreateUserKey(config.getCreateUserKey());
                item.setCreateUserName(config.getCreateUserName());
                item.setCreateTime(config.getCreateTime());
                item.setUpdateUserKey(config.getUpdateUserKey());
                item.setUpdateUserName(config.getUpdateUserName());
                item.setUpdateTime(config.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(configDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessCustomerOrderFollowCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerKey())
                || StringUtils.isBlank(param.getOrderKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerOrderFollowUpCreateCloudParam configCreateCloudParam = new CrmCustomerOrderFollowUpCreateCloudParam();
        configCreateCloudParam.setCorpKey(corpKey);
        configCreateCloudParam.setCustomerKey(param.getCustomerKey());
        configCreateCloudParam.setCustomerName(param.getCustomerName());
        configCreateCloudParam.setOrderKey(param.getOrderKey());
        configCreateCloudParam.setFollowUp(param.getFollowUp());
        configCreateCloudParam.setFollowUpDate(param.getFollowUpDate());
        configCreateCloudParam.setNextFollowUpRemindDate(param.getNextFollowUpRemindDate());
        configCreateCloudParam.setRemindStart(param.getRemindStart());
        configCreateCloudParam.setStatus(param.getStatus());
        configCreateCloudParam.setRemark(param.getRemark());
        configCreateCloudParam.setServerDate(param.getServerDate());
        configCreateCloudParam.setAssistingPersonnelKey(param.getAssistingPersonnelKey());
        configCreateCloudParam.setAssistingPersonnelName(param.getAssistingPersonnelName());
        configCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        configCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        ResponseContent responseContent = crmCustomerOrderFollowUpCloudService.create(configCreateCloudParam);
        if (null != responseContent && responseContent.isSuccess()) {
            String content = String.format(
                    "待办内容：%s，下次跟进提醒时间为：%s，提醒级别 %s星",
                    param.getFollowUp(),
                    SystemDateUtils.ofPattern(param.getNextFollowUpRemindDate(), "yyyy-MM-dd HH:mm:ss"),
                    param.getRemindStart());

            PublicTodoCreateCloudParam todoCreateCloudParam = new PublicTodoCreateCloudParam();
            todoCreateCloudParam.setCorpKey(YoukeyiSystemContext.getCorpPrimaryKey());
            todoCreateCloudParam.setUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            todoCreateCloudParam.setCustomerKey(param.getCustomerKey());
            todoCreateCloudParam.setContent(content);
            todoCreateCloudParam.setStatus(1);
            todoCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            todoCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
            publicTodoCloudService.create(todoCreateCloudParam);
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return rc;
    }

    @Override
    public ResponseContent update(BusinessCustomerOrderFollowUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerOrderFollowUpPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerOrderFollowUpUpdateCloudParam configUpdateCloudParam = new CrmCustomerOrderFollowUpUpdateCloudParam();
        configUpdateCloudParam.setCustomerFollowUpPrimaryKey(param.getCustomerOrderFollowUpPrimaryKey());
        configUpdateCloudParam.setCorpKey(corpKey);
        configUpdateCloudParam.setFollowUp(param.getFollowUp());
        configUpdateCloudParam.setFollowUpDate(param.getFollowUpDate());
        configUpdateCloudParam.setNextFollowUpRemindDate(param.getNextFollowUpRemindDate());
        configUpdateCloudParam.setRemindStart(param.getRemindStart());
        configUpdateCloudParam.setStatus(param.getStatus());
        configUpdateCloudParam.setRemark(param.getRemark());
        configUpdateCloudParam.setServerDate(param.getServerDate());
        configUpdateCloudParam.setAssistingPersonnelKey(param.getAssistingPersonnelKey());
        configUpdateCloudParam.setAssistingPersonnelName(param.getAssistingPersonnelName());
        configUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        configUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerOrderFollowUpCloudService.update(configUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessCustomerOrderFollowDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getCustomerOrderFollowUpPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerOrderFollowUpDeleteCloudParam configDeleteCloudParam = new CrmCustomerOrderFollowUpDeleteCloudParam();
        configDeleteCloudParam.setCustomerFollowUpPrimaryKeys(param.getCustomerOrderFollowUpPrimaryKeys());
        configDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        configDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerOrderFollowUpCloudService.delete(configDeleteCloudParam);
    }
}
